package ims.servlet;

import ims.constants.IMSConstants;
import ims.db.delete.product.DeleteProductDAO;
import ims.db.delete.product.DeleteProductDbRequest;
import ims.db.delete.product.DeleteProductDbResponse;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class DeleteProductServlet
 */
@WebServlet("/DeleteProductServlet")
public class DeleteProductServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DeleteProductServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try
		{
			String [] prod = request.getParameter("prodName").split(":");
			
			//Retrieve all product types
			DeleteProductDbRequest dbRequest = new DeleteProductDbRequest();
			dbRequest.setPid(prod[0]);
			dbRequest.setProdName(prod[1]);
			DeleteProductDAO db = new DeleteProductDAO();
			DeleteProductDbResponse dbResponse = (DeleteProductDbResponse) db.execute(dbRequest);
			
			String nextLoc = "/IMS/product?"+IMSConstants.RESPONSE_HEADER+"="+dbResponse.getMsg();      
			response.sendRedirect(response.encodeRedirectURL(nextLoc));
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

}
